import greenfoot.*;  

public class Timer extends Actor {
    private int time;
    private long startTime;
    private boolean running;

    public void act() {
        if (running) {
            time = (int) ((System.currentTimeMillis() - startTime) / 1000); // 计算经过的秒数
            updateImage();
        }
    }

    public void start() {
        running = true;
        startTime = System.currentTimeMillis(); // 记录开始时间
    }

    public void stop() {
        running = false; // 停止计时器
    }

    private void updateImage() {
        setImage(new GreenfootImage("时间: " + time + "秒", 24, Color.BLACK, Color.WHITE));
    }
}
